﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using Hidistro.Entities.Orders;
    using Hidistro.Entities.Promotions;
    using Hidistro.SaleSystem.Shopping;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.SaleSystem.CodeBehind.Common;
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    [ParseChildren(true)]
    public class VMemberOrderDetails : VMemberTemplatedNewWebControl
    {
        private Literal litActualPrice;
        private Literal litAddress;
        private Literal litBuildPrice;
        private Literal litCounponPrice;
        private Literal litDisCountPrice;
        private Literal litOrderDate;
        private Literal litOrderId;
        private OrderStatusLabel litOrderStatus;
        private Literal litPayCharge;
        private Literal litPayTime;
        private Literal litPhone;
        private Literal litRemark;
        private Literal litShippingCost;
        private Literal litShipTo;
        private Literal litShipToDate;
        private Literal litTax;
        private Literal litTotalPrice;
        private string orderId;
        private HtmlInputHidden orderStatus;
        private VshopTemplatedRepeater rptOrderProducts;
        private VshopTemplatedRepeater rptPromotions;
        private HtmlInputHidden txtOrderId;

        protected override void AttachChildControls()
        {
            this.orderId = this.Page.Request.QueryString["orderId"];
            this.litShipTo = (Literal) this.FindControl("litShipTo");
            this.litPhone = (Literal) this.FindControl("litPhone");
            this.litAddress = (Literal) this.FindControl("litAddress");
            this.litOrderId = (Literal) this.FindControl("litOrderId");
            this.litOrderDate = (Literal) this.FindControl("litOrderDate");
            this.litOrderStatus = (OrderStatusLabel) this.FindControl("litOrderStatus");
            this.rptOrderProducts = (VshopTemplatedRepeater) this.FindControl("rptOrderProducts");
            this.litTotalPrice = (Literal) this.FindControl("litTotalPrice");
            this.litPayTime = (Literal) this.FindControl("litPayTime");
            this.orderStatus = (HtmlInputHidden) this.FindControl("orderStatus");
            this.txtOrderId = (HtmlInputHidden) this.FindControl("txtOrderId");
            this.litRemark = (Literal) this.FindControl("litRemark");
            this.litShipToDate = (Literal) this.FindControl("litShipToDate");
            this.litShippingCost = (Literal) this.FindControl("litShippingCost");
            this.litCounponPrice = (Literal) this.FindControl("litCounponPrice");
            this.litBuildPrice = (Literal) this.FindControl("litBuildPrice");
            this.litDisCountPrice = (Literal) this.FindControl("litDisCountPrice");
            this.litActualPrice = (Literal) this.FindControl("litActualPrice");
            this.rptPromotions = (VshopTemplatedRepeater) this.FindControl("rptPromotions");
            this.litPayCharge = (Literal) this.FindControl("lblPayCharge");
            this.litTax = (Literal) this.FindControl("litTax");
            this.litPayCharge = (Literal) this.FindControl("litPayCharge");


            //OrderInfo orderInfo = ShoppingProcessor.GetOrderInfo(this.orderId);


            JSShop.Model.Hishop_Orders oredermodel = new Juse.Interface.UserOrder().GetModelByOrderId(this.orderId);
            List<JSShop.Model.Hishop_OrderItems> lists = new Juse.Interface.UserOrder().GetItemsByOrderId(this.orderId);
            if (oredermodel == null)
            {
                base.GotoResourceNotFound("此订单已不存在");
            }
            this.litShipTo.Text = ReplaceWithSpecialChar(oredermodel.ShipTo,1,0);
            this.litPhone.Text = ReplaceWithSpecialChar(oredermodel.CellPhone);
            int i = oredermodel.Address.Length / 2;
            this.litAddress.Text =  ReplaceWithSpecialChar(oredermodel.Address, i, 0);
            this.litOrderId.Text = this.orderId;
            this.litOrderDate.Text = oredermodel.OrderDate.ToString();
            this.litTotalPrice.SetWhenIsNotNull(Math.Round((decimal)oredermodel.Amount, 2).ToString());
            this.litOrderStatus.OrderStatusCode = oredermodel.OrderStatus;
            this.litPayTime.SetWhenIsNotNull((oredermodel.PayDate != DateTime.MinValue) ? oredermodel.PayDate.ToString() : "");
            this.orderStatus.SetWhenIsNotNull(((int)oredermodel.OrderStatus).ToString());
            this.txtOrderId.SetWhenIsNotNull(this.orderId.ToString());
            this.litCounponPrice.SetWhenIsNotNull(oredermodel.CouponValue.ToString());
            this.litShippingCost.SetWhenIsNotNull(oredermodel.AdjustedFreight.ToString());
            this.litShipToDate.SetWhenIsNotNull(oredermodel.ShipToDate);
            this.litBuildPrice.SetWhenIsNotNull(Math.Round((decimal)oredermodel.Amount,2).ToString());
            //this.litDisCountPrice.SetWhenIsNotNull(oredermodel.AdjustedDiscount.ToString("F0"));
            this.litActualPrice.SetWhenIsNotNull(Math.Round((decimal)(oredermodel.Amount + oredermodel.AdjustedFreight), 2).ToString());
            this.litRemark.SetWhenIsNotNull(oredermodel.Remark);
            this.litTax.SetWhenIsNotNull(oredermodel.Tax.ToString());
            this.litPayCharge.SetWhenIsNotNull(oredermodel.PayCharge.ToString());
            this.rptOrderProducts.DataSource = lists;
            this.rptOrderProducts.DataBind();
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            if (oredermodel.IsReduced)
            {
                list.Add(new KeyValuePair<string, string>(PromotionHelper.GetShortName(PromoteType.Reduced), oredermodel.ReducedPromotionName + string.Format(" 优惠：{0}", oredermodel.ReducedPromotionAmount.ToString())));
            }
            if (oredermodel.IsFreightFree)
            {
                list.Add(new KeyValuePair<string, string>(PromotionHelper.GetShortName(PromoteType.FullAmountSentFreight), string.Format("{0}", oredermodel.FreightFreePromotionName)));
            }
            if (oredermodel.IsSendTimesPoint)
            {
                list.Add(new KeyValuePair<string, string>(PromotionHelper.GetShortName(PromoteType.FullAmountSentTimesPoint), string.Format("{0}：送{1}倍", oredermodel.SentTimesPointPromotionName, oredermodel.TimesPoint.ToString())));
            }
            this.rptPromotions.DataSource = list;
            this.rptPromotions.DataBind();
            PageTitle.AddSiteNameTitle("订单详情");
        }

        private static string ReplaceWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = '*')
        {
            try
            {
                int lenth = value.Length - startLen - endLen;
                string replaceStr = value.Substring(startLen, lenth);
                string specialStr = string.Empty;
                for (int i = 0; i < replaceStr.Length; i++)
                {
                    specialStr += specialChar;
                }
                value = value.Replace(replaceStr, specialStr);
            }
            catch (Exception)
            {
                throw;
            }
            return value;
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-VMemberOrderDetails.html";
            }
            base.OnInit(e);
        }
    }
}

